package com.prd.controller;

import com.prd.constant.CheckStatus;
import com.prd.dto.SupervisorVo;
import com.prd.model.*;
import com.prd.service.EntGoalService;
import com.prd.util.AjaxResult;
import com.prd.util.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "目标管理")
@RestController
@RequestMapping("/api/entGoal")
public class EntGoalController extends BaseController {

    @Autowired
    private EntGoalService entGoalService;

    @ApiOperation("查询目标列表")
    @GetMapping("list")
    public TableDataInfo list(@ApiParam("日期") String day, @ApiParam("1:我发起、2：我接收、3：执行中") Integer type) {
        startPage();
        List<EntGoal> list = entGoalService.listInfo(day, type);
        return getDataPage(list);
    }

    @ApiOperation("添加目标")
    @PostMapping(value = "add")
    public AjaxResult add(@RequestBody EntGoal entGoal) {
        return toAjax(entGoalService.insertInfo(entGoal));
    }

    @ApiOperation("修改目标")
    @PostMapping(value = "update")
    public AjaxResult update(@RequestBody EntGoal entGoal) {
        return toAjax(entGoalService.updateInfo(entGoal));
    }

    @ApiOperation("查询目标详情")
    @GetMapping(value = "details/{id}")
    public AjaxResult details(@PathVariable("id") Long id) {
        return AjaxResult.success(entGoalService.details(id));
    }

    @ApiOperation("查询目标人员权限")
    @GetMapping("jurisdiction/{goalId}")
    public AjaxResult jurisdiction(@PathVariable("goalId") Long goalId) {
        return AjaxResult.success(entGoalService.jurisdiction(goalId));
    }

    @ApiOperation("审核通过")
    @PostMapping(value = "pass")
    public AjaxResult pass(@RequestBody EntGoalCheck entGoalCheck) {
        entGoalCheck.setType(CheckStatus.TWO.getCode());
        return toAjax(entGoalService.pass(entGoalCheck));
    }

    @ApiOperation("审核不通过")
    @PostMapping(value = "noPass")
    public AjaxResult noPass(@RequestBody EntGoalCheck entGoalCheck) {
        entGoalCheck.setType(CheckStatus.THREE.getCode());
        return toAjax(entGoalService.pass(entGoalCheck));
    }

    @ApiOperation("由我负责执行")
    @PostMapping(value = "execute/{goalId}")
    public AjaxResult execute(@PathVariable("goalId") Long goalId) {
        return toAjax(entGoalService.execute(goalId));
    }

    @ApiOperation("目标责任分解")
    @PostMapping(value = "executeResolve")
    public AjaxResult executeResolve(@RequestBody EntGoalStage entGoalStage) {
        return toAjax(entGoalService.executeResolve(entGoalStage));
    }

    @ApiOperation("查询目标不分期执行汇报")
    @GetMapping("listExecutiveNoStage/{goalId}")
    public AjaxResult listExecutiveNoStage(@PathVariable("goalId") Long goalId) {
        return AjaxResult.success(entGoalService.listExecutiveNoStage(goalId));
    }

    @ApiOperation("查询目标分期执行汇报")
    @GetMapping("listExecutiveStage/{stageId}")
    public AjaxResult listExecutiveStage(@PathVariable("stageId") Long stageId) {
        return AjaxResult.success(entGoalService.listExecutiveStage(stageId));
    }

    @ApiOperation("添加目标分解责任")
    @PostMapping(value = "addDecompose")
    public AjaxResult addDecompose(@RequestBody EntGoal entGoal) {
        return toAjax(entGoalService.addDecompose(entGoal));
    }

    @ApiOperation("查询目标分解责任列表")
    @GetMapping("listDecompose/{decompose}/{goalId}")
    public AjaxResult list(@ApiParam("分解责任第几层") Integer decompose, @ApiParam("目标ID") @PathVariable("goalId") Long goalId) {
        return AjaxResult.success(entGoalService.listDecompose(decompose, goalId));
    }

    @ApiOperation("变更目标责任人")
    @PostMapping(value = "updateDuty")
    public AjaxResult updateDuty(@RequestBody EntGoal entGoal) {
        if (entGoal.getId() == null || StringUtils.isBlank(entGoal.getReasons()) || StringUtils.isBlank(entGoal.getOverall())) {
            return AjaxResult.error("参数不能为空！");
        }
        return toAjax(entGoalService.updateDuty(entGoal));
    }

    @ApiOperation("添加督导审核结果")
    @PostMapping(value = "addSupervisor")
    public AjaxResult addSupervisor(@RequestBody SupervisorVo supervisorVo) {
        return toAjax(entGoalService.addSupervisor(supervisorVo));
    }

    @ApiOperation("撤销目标")
    @PostMapping(value = "revocation/{goalId}")
    public AjaxResult revocation(@PathVariable("goalId") Long goalId) {
        return toAjax(entGoalService.revocation(goalId));
    }

    @ApiOperation("增加抄送人")
    @PostMapping(value = "addMake/{goalId}/{makes}")
    public AjaxResult addMake(@ApiParam("目标ID") @PathVariable("goalId") Long goalId, @ApiParam("抄送人（多个,分开）") @PathVariable("makes") Long[] makes) {
        return toAjax(entGoalService.addMake(goalId, makes));
    }
}
